﻿using DAL.Interface;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Model;
using Model.Entity;
using System.Linq.Expressions;

namespace DAL
{
    /// <summary>
    /// 基础数据访问层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseDAL<T> : IBaseDAL<T> where T : EntityBase
    {
        private readonly TianXiaHuiForumManagementSystemDbContext _dbContext;
        public BaseDAL(TianXiaHuiForumManagementSystemDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 获取基础上下文对象
        /// </summary>
        /// <returns></returns>
        public DbContext GetBaseDBContext()
        {
            return _dbContext;
        }
        /// <summary>
        /// 获取上下文对象
        /// </summary>
        /// <returns></returns>
        public TianXiaHuiForumManagementSystemDbContext GetDBContext()
        {
            return _dbContext;
        }
        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        public async Task<IDbContextTransaction> BeginTransactionAsync()
        {
            return await _dbContext.Database.BeginTransactionAsync();
        }
        /// <summary>
        /// 创建实体并返回
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<T> AddEntityAndReturnAsync(T entity)
        {
            var result = await _dbContext.AddAsync(entity);
            await _dbContext.SaveChangesAsync();
            return result.Entity;
        }
        /// <summary>
        /// 创建实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> AddEntityAsync(T entity)
        {
            await _dbContext.AddAsync(entity);
            return await _dbContext.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DeleteEntityAsync(T entity)
        {
            _dbContext.Remove(entity);
            return await _dbContext.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteEntityAsync(int id)
        {
            T? entity = await _dbContext.Set<T>().FindAsync(id);
            if (entity == null)
            {
                return false;
            }
            _dbContext.Remove(entity);
            return await _dbContext.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> EditEntityAsync(T entity)
        {
            _dbContext.Update(entity);
            return await _dbContext.SaveChangesAsync() > 0;
        }
        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetEntityAllAsync()
        {
            return await Task.Run(() => _dbContext.Set<T>());
        }
        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetEntityAllAsync(Expression<Func<T, bool>> whereLambda)
        {
            return await Task.Run(() =>_dbContext.Set<T>().Where(whereLambda));
        }
    }
}
